package com.qingzhu.controller.adminserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qingzhu.pojo.Goods;
import com.qingzhu.pojo.Item;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.GoodsService;
import com.qingzhu.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/goods")
public class AdminSelectGoodsController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("后台商品查询..");

        String server = req.getParameter("server");

        if (server.equals("goods")) {
            selectGoodsAll(req,resp);
        }else if (server.equals("items")) {
            selectItemsfromGoodsId(req,resp);
        }else if (server.equals("item")) {
            selectItemfromId(req,resp);
        }else if (server.equals("goodsFromId")) {
            selectGoodsfromId(req,resp);
        }
    }


    // 查询所有的商品信息（SPU）
    public void selectGoodsAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        Page<Goods> goodsPage = goodsService.findGoodsByLike(null,Integer.parseInt(pageNum), Integer.parseInt(pageSize), true,false);
        ResultVo resultVo = new ResultVo(200,new PageInfo<>(goodsPage));
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 查询商品所有的详细规格产品（SKU）
    public void selectItemsfromGoodsId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsId = req.getParameter("goodsId");
        String excludeDelStr = req.getParameter("excludeDel");
        // 如果是true则只查询正常上架的产品；如果是flase则查询所有产品，包括已下架和已删除的
        boolean excludeDel = Boolean.valueOf(excludeDelStr).booleanValue();
        List<Item> itemList = goodsService.findItemsFromGoodsId(Integer.parseInt(goodsId),excludeDel);
        ResultVo resultVo = new ResultVo(200,itemList);
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 查询单品信息
    public void selectItemfromId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String itemId = req.getParameter("itemId");
        Item item = goodsService.findItemfromId(Integer.parseInt(itemId));
        ResultVo resultVo = new ResultVo(200,item);
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 根据ID精确查询商品信息
    public void selectGoodsfromId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsId = req.getParameter("goodsId");
        Goods goods = goodsService.findGoodsFromId(Integer.parseInt(goodsId),false,false);
        ResultVo resultVo = new ResultVo(200,goods);
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
